package com.five.bi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.bi.enums.FunctionEnum;
import com.five.bi.mapper.DataSetMapper;
import com.five.bi.model.DataSetDO;
import com.five.bi.model.SchemaDO;
import com.five.bi.model.request.ListDataSetRequest;
import com.five.bi.model.request.SaveDataSetRequest;
import com.five.bi.model.vo.DataSetVO;
import com.five.bi.model.vo.SchemaVO;
import com.five.bi.service.DataSetService;
import com.five.bi.service.SchemaService;
import com.five.bi.utils.IpUtil;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author xuzhihong
 * @Version 1.0
 * @Date 2022/8/3
 */
@Service
public class DataSetServiceImpl implements DataSetService {

    @Resource
    private DataSetMapper dataSetMapper;

    @Autowired
    private SchemaService schemaService;

    @Override
    public Map<String, Object> list(ListDataSetRequest listDataSetRequest) {

        Page<DataSetDO> pageInfo = new Page<>(listDataSetRequest.getPage(), listDataSetRequest.getPageSize());


        IPage<DataSetDO> dataSetIPage = dataSetMapper.selectPage(pageInfo, new QueryWrapper<DataSetDO>()
                .eq("create_user", listDataSetRequest.getCreateUser())
                .like("name",listDataSetRequest.getKeyword())
                .orderBy(true,listDataSetRequest.getOrder(),listDataSetRequest.getOrderBy()));

        List<DataSetDO> dataSetDOList = dataSetIPage.getRecords();

        List<DataSetVO> dataSetVOList = dataSetDOList.stream().map(dataSetDO -> {
            DataSetVO dataSetVO = new DataSetVO();
            BeanUtils.copyProperties(dataSetDO, dataSetVO);
            List<SchemaVO> schemaVOList = schemaService.listBySetId(dataSetDO.getId());
            dataSetVO.setSchema(schemaVOList);
            return dataSetVO;
        }).collect(Collectors.toList());

        Map<String,Object> pageMap = new HashMap<>(3);
        System.out.println(dataSetIPage.getTotal());
        pageMap.put("totalCount",dataSetIPage.getTotal());
        pageMap.put("totalPage",dataSetIPage.getPages());
        pageMap.put("dataSetList",dataSetVOList);

        return pageMap;
    }

    @Override
    public String save(SaveDataSetRequest saveDataSetRequest, HttpServletRequest request) {
        DataSetDO dataSetDO = new DataSetDO();
        BeanUtils.copyProperties(saveDataSetRequest, dataSetDO);

        dataSetDO.setCreateTime(new DateTime().toString());
        dataSetDO.setStatus(0);
        dataSetDO.setCreateUser(IpUtil.getIpAddress(request));
        dataSetMapper.insert(dataSetDO);

        String dataSetId = dataSetDO.getId();

        List<SchemaDO> schemaDOList = saveDataSetRequest.getSchema();

        for(SchemaDO schemaDO: schemaDOList){
            schemaDO.setSetId(dataSetId);
        }
        schemaService.insertBatch(schemaDOList);

        return dataSetId;
    }

    @Override
    public Map<String, Object> info(String dataSetId) {

        Map<String, Object> dataSetInfo = new HashMap<>(3);

        List<SchemaVO> schemaVOList = schemaService.listBySetId(dataSetId);

        List<SchemaVO> dimensionList = new ArrayList<>();
        List<SchemaVO> metricList = new ArrayList<>();

        for (SchemaVO schemaVO:schemaVOList){
            if("String".equals(schemaVO.getType())){
                dimensionList.add(schemaVO);
            }else {
                metricList.add(schemaVO);
            }
        }

        List<HashMap<String, String>> functionList = listFunction();

        dataSetInfo.put("dimensionList",dimensionList);
        dataSetInfo.put("metricList",metricList);
        dataSetInfo.put("functionList",functionList);
        return dataSetInfo;
    }

    private List<HashMap<String, String>> listFunction() {
        List<HashMap<String, String>> list = new ArrayList<>();
        for (FunctionEnum functionEnum : EnumSet.allOf(FunctionEnum.class)) {
            HashMap<String, String> map = new HashMap<>();
            map.put("name", functionEnum.getName());
            map.put("code", functionEnum.getValue());
            list.add(map);
        }
        return list;
    }

    @Override
    public int deleteById(String id) {
        return dataSetMapper.deleteById(id);
    }
}
